package com.monitor.system.service.impl;

import com.monitor.common.utils.DateUtils;
import com.monitor.system.domain.AbnormalCollectionAlarm;
import com.monitor.system.domain.vo.AbnormalCollectionAlarmVo;
import com.monitor.system.mapper.AbnormalCollectionAlarmMapper;
import com.monitor.system.service.IAbnormalCollectionAlarmService;
import com.monitor.system.service.IApplicationAbnormalManagementService;
import com.monitor.system.service.IMonitoringManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * 异常采集告警Service业务层处理
 * 
 * @author monitor
 * @date 2024-06-06
 */
@Service
public class AbnormalCollectionAlarmServiceImpl implements IAbnormalCollectionAlarmService 
{
    @Autowired
    private AbnormalCollectionAlarmMapper abnormalCollectionAlarmMapper;

    @Resource
    private IMonitoringManagementService monitoringManagementService;
    @Resource
    private IApplicationAbnormalManagementService applicationAbnormalManagementService;

    /**
     * 查询异常采集告警
     * 
     * @param id 异常采集告警主键
     * @return 异常采集告警
     */
    @Override
    public AbnormalCollectionAlarm selectAbnormalCollectionAlarmById(Long id)
    {
        return abnormalCollectionAlarmMapper.selectAbnormalCollectionAlarmById(id);
    }

    /**
     * 查询异常采集告警列表
     * 
     * @param abnormalCollectionAlarm 异常采集告警
     * @return 异常采集告警
     */
    @Override
    public List<AbnormalCollectionAlarm> selectAbnormalCollectionAlarmList(AbnormalCollectionAlarm abnormalCollectionAlarm)
    {
        return abnormalCollectionAlarmMapper.selectAbnormalCollectionAlarmList(abnormalCollectionAlarm);
    }

    /**
     * 新增异常采集告警
     * 
     * @param abnormalCollectionAlarm 异常采集告警
     * @return 结果
     */
    @Override
    public int insertAbnormalCollectionAlarm(AbnormalCollectionAlarm abnormalCollectionAlarm)
    {
        abnormalCollectionAlarm.setCreateTime(DateUtils.getNowDate());
        return abnormalCollectionAlarmMapper.insertAbnormalCollectionAlarm(abnormalCollectionAlarm);
    }

    /**
     * 修改异常采集告警
     * 
     * @param abnormalCollectionAlarm 异常采集告警
     * @return 结果
     */
    @Override
    public int updateAbnormalCollectionAlarm(AbnormalCollectionAlarm abnormalCollectionAlarm)
    {
        abnormalCollectionAlarm.setUpdateTime(DateUtils.getNowDate());
        return abnormalCollectionAlarmMapper.updateAbnormalCollectionAlarm(abnormalCollectionAlarm);
    }

    /**
     * 批量删除异常采集告警
     * 
     * @param ids 需要删除的异常采集告警主键
     * @return 结果
     */
    @Override
    public int deleteAbnormalCollectionAlarmByIds(Long[] ids)
    {
        return abnormalCollectionAlarmMapper.deleteAbnormalCollectionAlarmByIds(ids);
    }

    /**
     * 删除异常采集告警信息
     * 
     * @param id 异常采集告警主键
     * @return 结果
     */
    @Override
    public int deleteAbnormalCollectionAlarmById(Long id)
    {
        return abnormalCollectionAlarmMapper.deleteAbnormalCollectionAlarmById(id);
    }

    /**
     * 可视化分析查询
     *
     * @param vo
     * @return 结果
     */
    @Override
    public AbnormalCollectionAlarmVo selectAbnormalCollectionAlarmListNew(AbnormalCollectionAlarmVo vo) {
        AbnormalCollectionAlarmVo obj = abnormalCollectionAlarmMapper.selectAbnormalCollectionAlarmListNew(vo);
        BigDecimal faultTotal = new BigDecimal(0);
        BigDecimal abnormalTotal = new BigDecimal(0);
        BigDecimal alarmTotal = new BigDecimal(0);
        BigDecimal delayTotal = new BigDecimal(0);
        BigDecimal total = new BigDecimal(0);;

        if(obj != null ) {
            // 故障总数
            faultTotal = obj.getSmtpCrisis().add(obj.getSnmpCrisis()).add(obj.getWindowsLogCrisis());
            // 异常总数
            abnormalTotal = obj.getSmtpAlarm().add(obj.getSnmpAlarm()).add(obj.getWindowsLogAlarm());
            // 告警总数
            alarmTotal = obj.getSmtpNotice().add(obj.getSnmpNotice()).add(obj.getWindowsLogNotice());
            // 延时总数
            delayTotal = obj.getSmtpUnknown().add(obj.getSnmpUnknown()).add(obj.getWindowsLogUnknown());
            // 总数
            total = faultTotal.add(abnormalTotal).add(alarmTotal).add(delayTotal);
            obj.setFaultTotal(faultTotal);
            obj.setAbnormalTotal(abnormalTotal);
            obj.setAlarmTotal(alarmTotal);
            obj.setDelayedTotal(delayTotal);
            obj.setTotal(total);
            // 百分比
            if (total.compareTo(BigDecimal.ZERO) > 0) {
                obj.setFaultPercent((faultTotal.multiply(new BigDecimal(100)).divide(total, 1,BigDecimal.ROUND_HALF_UP)));
                obj.setAlarmPercent((abnormalTotal.multiply(new BigDecimal(100)).divide(total, 1,BigDecimal.ROUND_HALF_UP)));
                obj.setAbnormalPercent((alarmTotal.multiply(new BigDecimal(100)).divide(total, 1,BigDecimal.ROUND_HALF_UP)));
                obj.setDelayedPercent((delayTotal.multiply(new BigDecimal(100)).divide(total, 1,BigDecimal.ROUND_HALF_UP)));
            } else {
                // 告警百分比
                obj.setFaultPercent(new BigDecimal(0));
                // 告警
                obj.setAlarmPercent(new BigDecimal(0));
                // 异常百分比
                obj.setAbnormalPercent(new BigDecimal(0));
                // 延时百分比
                obj.setDelayedPercent(new BigDecimal(0));
            }
            return obj;
        } else {
            AbnormalCollectionAlarmVo objVo = new AbnormalCollectionAlarmVo();
            objVo.setTotal(total);
            objVo.setFaultTotal(faultTotal);
            objVo.setAbnormalTotal(abnormalTotal);
            objVo.setAlarmTotal(alarmTotal);
            objVo.setDelayedTotal(delayTotal);
            // 告警百分比
            objVo.setFaultPercent(new BigDecimal(0));
            // 告警
            objVo.setAlarmPercent(new BigDecimal(0));
            // 异常百分比
            objVo.setAbnormalPercent(new BigDecimal(0));
            // 延时百分比
            objVo.setDelayedPercent(new BigDecimal(0));
            return objVo;
        }
    }
}
